﻿using MvvmOrm.DbmsModules;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Linq;
using MvvmOrm.Models.Column;
using System.Collections.Generic;
using DatabaseItem = MvvmOrm.Models.DatabaseItem.Row;
using System.IO;

namespace MvvmOrm.Tests
{
    [TestClass()]
    public class SqliteModuleTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void InstanceTest()
        {
            SqliteModule actual;
            actual = SqliteModule.Instance;
            Assert.IsNotNull(actual);
        }

        [TestMethod()]
        public void ConnectTest()
        {
            Models.MainViewModel mainViewModel = Common.PrepareSqliteContext();
            SqliteModule sqliteAnalyser = SqliteModule.Instance;
            sqliteAnalyser.Connect(mainViewModel.DB);
            sqliteAnalyser.Disconnect();
        }

        [TestMethod()]
        public void DisconnectTest()
        {
            Models.MainViewModel mainViewModel = Common.PrepareSqliteContext();
            SqliteModule sqliteAnalyser = SqliteModule.Instance;
            sqliteAnalyser.Connect(mainViewModel.DB);
            sqliteAnalyser.Disconnect();
        }

        [TestMethod()]
        public void AnalyseTest()
        {
            Models.MainViewModel mainViewModel = Common.PrepareSqliteContext();
            SqliteModule sqliteAnalyser = SqliteModule.Instance;
            sqliteAnalyser.Connect(mainViewModel.DB);
            sqliteAnalyser.AnalyseDatabase(mainViewModel.DB);
            sqliteAnalyser.Disconnect();
            Assert.AreEqual(mainViewModel.DB.Tables.Count(), 8);
            DatabaseItem databaseItem = mainViewModel.DB.Tables.First(_ => _.Name == "Version");
            Assert.IsTrue(databaseItem.Name == "Version");
            Assert.IsTrue(databaseItem.NameSanitized == "Version");
            Assert.IsTrue(databaseItem.Child_Columns.Count == 1);
            Assert.IsTrue(databaseItem.PrimaryKeys.Count() == 0);
        }
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"C:\Projects\MvvmOrm\MvvmOrm.Tests\Resources\MockTableColumnsSqlite.csv", "MockTableColumnsSqlite#csv", DataAccessMethod.Sequential)]
        [TestMethod()]
        public void MapColumnInfoTest()
        {
            //bool isView = false;
            //Row actual;
            //actual = SqliteModule_Accessor.MapColumnInfo(TestContext.DataRow, isView);
            //Assert.AreEqual(actual.Name, TestContext.DataRow["COLUMN_NAME"] as string);
            //Assert.AreEqual(actual.MaxLength, (int)TestContext.DataRow["CHARACTER_MAXIMUM_LENGTH"]);
            //Assert.AreEqual(actual.IsPrimaryKey, (bool)TestContext.DataRow["PRIMARY_KEY"]); ;
            //Assert.AreEqual(actual.DbType, (int)SqliteModule_Accessor.GetColumnDbType(TestContext.DataRow["DATA_TYPE"] as string, (int)TestContext.DataRow["CHARACTER_MAXIMUM_LENGTH"])); ;
            //Assert.AreEqual(actual.IsAutoIncrement, (bool)TestContext.DataRow["AUTOINCREMENT"]); ;
            //Assert.AreEqual(actual.IsReadOnly, actual.IsAutoIncrement); ;
            //Assert.AreEqual(actual.Nullable, (bool)TestContext.DataRow["IS_NULLABLE"]); ;
        }

        [TestMethod()]
        public void AdditionalReferencedAssembliesTest()
        {
            SqliteModule target = SqliteModule.Instance;
            IEnumerable<string> actual;
            actual = target.AdditionalReferencedAssemblies;
            Assert.IsTrue(actual.Count() == 1);
        }

        [TestMethod()]
        public void GenerateCodeTest()
        {
            Models.MainViewModel mainViewModel = Common.PrepareSqliteContext();
            mainViewModel.DB.OutputDirectory = Common.GetTemporaryDirectory();
            SqliteModule target = SqliteModule.Instance;
            target.AnalyseDatabase(mainViewModel.DB);
            Dictionary<string, string> sources = target.GenerateCode(mainViewModel.DB);
            Assert.AreEqual(sources.Count, 12);
            //Assert.IsTrue(Directory.EnumerateFiles(mainViewModel.DB.OutputDirectory).Count() > 0);
        }
    }
}
